import Vue from 'vue'
import App from './App'
import MescrollBody from "@/common/mescroll-uni/mescroll-body.vue";
import Mixin from "@/common/mescroll-uni/mescroll-mixins.js";
// import uView from "uview-ui"

import {
	gotopage
} from '@/common/gotopage.js';
// 配置公共方法
Vue.prototype.gotoPage = gotopage;

// Vue.use(uView);
// 配置公共方法
import common from '@/common/common.js'
Vue.prototype.$noMultipleClicks = common.noMultipleClicks;


Vue.prototype.req = function(param, backpage, backtype) {
	uni.getNetworkType({
		success: function(res) {
			if (res.networkType == 'none') {
				uni.showToast({
					title: '网络连接异常，请检查网络',
					icon: 'none',
				})
				return
			}
		}
	});
	var _self = this,
		url = param.url,
		method = 'POST',
		header = {},
		data = param.data || {},
		Loading = param.Loading || false;

	var token = uni.getStorageSync('token')
	// 我
	// var token = 'f9fdfb5ed1ef1216a6eda8a841e3970e0776c3ea';
	// 客户
	// var token = '801abe6ec37a36b3ff37ded44be13c7bf06b629e';

	//拼接完整请求地址
	var requestUrl = this.siteBaseUrl + url;
	if (method) {
		method = method.toUpperCase(); //小写改为大写
		// console.log(token,11)
		if (method == "POST") {
			header = {
				'content-type': "application/x-www-form-urlencoded",
				'token': token
			};
		} else {
			header = {
				'content-type': "application/json"
			};
		}
	} else {
		method = "GET";
		header = {
			'content-type': "application/json"
		};
	}
	//用户交互:加载圈
	// console.log(!Loading)
	if (!Loading) {
		uni.showLoading({
			title: '加载中...'
		});
	}
	//网络请求

	uni.request({
		url: requestUrl,
		method: method,
		header: header,
		data: data,
		success: res => {
			// console.log(res.data)
			if (res.data.status == 1) { //返回结果码code判断:1成功,-1错误
				// uni.showToast({
				// 	title: res.data.msg,
				// 	icon: 'none',
				// })

			} else if (res.data.status == -9) {
				let pages = getCurrentPages();
				console.log(pages[pages.length - 1].route)
				// uni.setStorageSync('page', pages[pages.length - 1].route)
				uni.showToast({
					title: res.data.msg,
					icon: 'none',
					success() {
						setTimeout(() => {
							uni.navigateTo({
								url: '/pages/user/bangding'
							})
						}, 1500)
					},
				})

			} else if (res.data.status == 0) {
				setTimeout(function() {
					uni.showToast({
						title: res.data.msg,
						icon: 'none',
					})
				}, 500)
			} else if (res.data.status < 0) {
				var pages = getCurrentPages();
				for (var a = 0; a < pages.length; a++) {
					console.log(pages[a].route)
					if (pages[a].route == "pages/user/index") {
						uni.setStorageSync('lgurl', pages[a].route)
						uni.setStorageSync('lgurldata', JSON.stringify(pages[a].options))
					}
				}
				uni.showToast({
					title: '请先登录',
					icon: 'none',
				})
				uni.redirectTo({
					url: '/pages/user/login'
				})

			} else {
				return;
			}
			typeof param.success == "function" && param.success(res.data);

		},
		fail: (e) => {
			console.log("网络请求fail:" + JSON.stringify(e));
			typeof param.fail == "function" && param.fail(e.data);
		},
		complete: () => {
			uni.hideLoading();
			typeof param.complete == "function" && param.complete();
			return;
		}
	});

}


// 测试
// Vue.prototype.siteBaseUrl = 'https://ykmkp.shequtuangou.vip/api/';
// Vue.prototype.$z_img = 'https://ykmkp.shequtuangou.vip/static/zcq/';
// 正式 h5
/* Vue.prototype.siteBaseUrl = '/api/';
Vue.prototype.$z_img = 'https://qt.yhemall.com/static/zcq/'; */
// 正式 小程序
Vue.prototype.siteBaseUrl = 'https://qt.yhemall.com/api/';
Vue.prototype.$z_img = 'https://qt.yhemall.com/static/zcq/';


Vue.config.productionTip = false
Vue.component("mescroll-body", MescrollBody);
Vue.mixin(Mixin);
App.mpType = 'app'

const app = new Vue({
	...App
})
app.$mount()